package com.project.livetv.fragment;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ListView;

import com.project.livetv.R;
import com.project.livetv.adapter.MyFragmentPageAdapter;
import com.project.livetv.adapter.MyHotTabAdapter;
import com.project.livetv.adapter.MyPageAdapter;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

/**
 * Created by li on 2016/5/9.
 */
public class HotFragment extends BaseFragment implements ViewPager.OnPageChangeListener{

    private ViewPager hotViewpager;
    private ListView mListView;
    private List<View> views;
    private MyPageAdapter pageradapter;
    private List<String> data;
    private MyHotTabAdapter topAdapter;
    private int position;

    private Timer timer;

    List<ImageView> pointList;
    private ImageView first_icon,second_icon,third_icon,forth_icon,fivith_icon,sixth_icon,seventh_icon;
    private Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case 1:
                    position++;
                    hotViewpager.setCurrentItem(position);
                    break;
            }
        }
    };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        views=new ArrayList<>();
        data=new ArrayList<>();
        pointList=new ArrayList<>();
        pageradapter=new MyPageAdapter(views);
        for (int i = 0; i <20 ; i++) {
            String hh="你好我的朋友"+i;
            data.add(hh);
        }
        topAdapter=new MyHotTabAdapter(getContext(),data);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView=inflater.inflate(R.layout.hot_fragment_layout,container,false);
        hotViewpager = (ViewPager) rootView.findViewById(R.id.viewpager_hottab_position);
        mListView= (ListView) rootView.findViewById(R.id.listView_hottab_position);
       // first_icon,second_icon,third_icon,forth_icon,fivith_icon,sixth_icon,seventh_icon;

        first_icon= (ImageView) rootView.findViewById(R.id.first_point);
        second_icon= (ImageView) rootView.findViewById(R.id.second_point);
        third_icon= (ImageView) rootView.findViewById(R.id.third_point);
        forth_icon= (ImageView) rootView.findViewById(R.id.forth_point);
        fivith_icon= (ImageView) rootView.findViewById(R.id.fivth_point);
        sixth_icon= (ImageView) rootView.findViewById(R.id.sixth_point);
        seventh_icon= (ImageView) rootView.findViewById(R.id.seventh_point);
        first_icon.setSelected(true);
       // pointList.add(seventh_icon);
        pointList.add(first_icon);
        pointList.add(second_icon);
        pointList.add(third_icon);
        pointList.add(forth_icon);
        pointList.add(fivith_icon);
        pointList.add(sixth_icon);
        pointList.add(seventh_icon);
        pointList.add(first_icon);
       // hotViewpager.setCurrentItem(1);
        timer=new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                Message message = Message.obtain();
                message.what = 1;
                handler.sendEmptyMessageDelayed(message.what, 2000);
            }
        }, 0, 2000);
        setData();
        return rootView;
    }

    private void setData() {

        for (int i = 0; i <7 ; i++) {
            View view=LayoutInflater.from(getContext()).inflate(R.layout.tab_hot_layout,null);
            ImageView imageview= (ImageView) view.findViewById(R.id.imageview_tab_viewpager_position);
            views.add(view);
        }
        hotViewpager.setAdapter(pageradapter);

        mListView.setAdapter(topAdapter);
        setListViewHeight(mListView);
        hotViewpager.addOnPageChangeListener(this);


    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        this.position=position;
        for (int i = 0; i <pointList.size() ; i++) {
            pointList.get(i).setSelected(i==position);
        }

    }

    @Override
    public void onPageScrollStateChanged(int state) {
        if (position == views.size() - 1 && state == ViewPager.SCROLL_STATE_IDLE) {
            position = 0;
            hotViewpager.setCurrentItem(position, false);

        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        hotViewpager.removeOnPageChangeListener(this);
        timer.cancel();
        timer = null;
    }
    public void setListViewHeight(ListView listView) {

        // 获取ListView对应的Adapter

        ListAdapter listAdapter = listView.getAdapter();

        if (listAdapter == null) {
            return;
        }
        int totalHeight = 0;
        for (int i = 0, len = listAdapter.getCount(); i < len; i++) { // listAdapter.getCount()返回数据项的数目
            View listItem = listAdapter.getView(i, null, listView);
            listItem.measure(0, 0); // 计算子项View 的宽高
            totalHeight += listItem.getMeasuredHeight(); // 统计所有子项的总高度
        }

        ViewGroup.LayoutParams params = listView.getLayoutParams();
        params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
        listView.setLayoutParams(params);
    }
}
